
import { useEffect } from 'react';
import { ModalForm, ProFormText, ProFormSwitch } from '@ant-design/pro-form';
import { message } from 'antd';
import { connect } from 'umi';

function SysDictDetail(props: {
  isEdit: boolean;
  currentRow: any;
  formModalVisible: boolean;
  handleModalVisible: any;
  setDataSource: any;
  dataSource: any;
  actionRef: any;
  formRef: any;
  [key: string]: any;
}) {
  const { isEdit, currentRow, formModalVisible, handleModalVisible, actionRef, formRef, dispatch } = props;

  const formItemLayout = { labelCol: { span: 5 }, wrapperCol: { span: 17 } };

  useEffect(() => {
    // 状态
    if (currentRow) {
      currentRow.status = currentRow.status === 1;
    }
    // 回显
    if(isEdit){
        formRef?.current?.setFieldsValue(currentRow);
    }
  }, [isEdit]);

  return (
    <ModalForm<{
      name: string;
      desc?: string;
    }>
      title={isEdit ? '编辑' : '新建'}
      width="600px"
      formRef={formRef}
      visible={formModalVisible}
      onVisibleChange={handleModalVisible}
      layout='horizontal'
      {...formItemLayout}
      modalProps={{
        maskClosable: false,
      }}
      onFinish={async (value) => {

        const fromData = {
          ...value,
          status: (value.status ? 1 : 0),
          id: isEdit ? currentRow.id : undefined,
          username: isEdit ? undefined : value.username,
        }
        const hide = message.loading('操作中');
        // 修改
        dispatch({
          type: isEdit ? 'sysDict/updateObject' : 'sysDict/saveObject',
          payload: fromData,
          callback: (res) => {
            if (res.code === 1) {
              hide();
              handleModalVisible(false);
              message.success('操作成功');
              actionRef?.current?.reload(); // 此处reload 不可省
              formRef?.current?.resetFields();// 表单清空
              
            } else {
              hide();

              message.error(res.msg);
              return false;
            }

          }
        });
      }}

    >
     

     
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="类型编码 根据编码查询一类数据出来，供下拉等情形使用"
                    width="md"
                    name="typeCode"
                  />
      
                                        
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="类型名称"
                    width="md"
                    name="typeName"
                  />
      
                                        
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="属性名称 实际的属性名称"
                    width="md"
                    name="name"
                  />
      
                                        
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="排序"
                    width="md"
                    name="sort"
                  />
      
                                        
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="0：正常 1：作废"
                    width="md"
                    name="state"
                  />
      
                                        
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="备注信息"
                    width="md"
                    name="remark"
                  />
      
                                        
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="创建人"
                    width="md"
                    name="createBy"
                  />
      
                                        
                  <ProFormText
                    rules={[
                      {
                        max: 20,
                        min: 1,
                        message: '请输入1-20个字符！',
                        required: true,
                      },
                    ]}
                    label="更新人"
                    width="md"
                    name="updateBy"
                  />
      
                                        
 
    </ModalForm>
  );
}


export default connect(
  ({ loading }: { index: any; loading: any }) => ({

    loading: loading.models.sysDict,
  }),
)(SysDictDetail);
